package com.gamatechno.adapter;

import java.util.ArrayList;

import com.gamatechno.solodestination.model.Item;
import com.gamatechno.solodestination.model.Item2;
import com.gamatechno.solodestination.R;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CustomSubGridView2 extends ArrayAdapter<Item2> {
	Context context;
	int layoutResourceId;
	ArrayList<Item2> data = new ArrayList<Item2>();
	Activity act;
	public ImageLoader2 imageLoader; 
	public CustomSubGridView2(Context context, int layoutResourceId,
			ArrayList<Item2> data, Activity activit) {
		super(context, layoutResourceId, data);
		
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.data = data;
		act= activit;
		imageLoader = new ImageLoader2(act);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		RecordHolder holder = null;
		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);
			holder = new RecordHolder();
			holder.txtTitle = (TextView) row.findViewById(R.id.Titel);
			holder.Komen = (TextView) row.findViewById(R.id.komen);
			holder.Rating = (RatingBar) row.findViewById(R.id.ratingbar);
			holder.bg = (RelativeLayout) row.findViewById(R.id.background);
			row.setTag(holder);
		} else {
			holder = (RecordHolder) row.getTag();
		}
		Item2 item = data.get(position);
		holder.txtTitle.setText(item.getTitle());
		holder.Komen.setText(item.getKomen());
		
		try {
			holder.Rating.setRating(Float.parseFloat(item.getRating()));
		} catch (Exception e) {
			// TODO: handle exception
			holder.Rating.setRating(0);
		}
		
		//holder.bg.setBackgroundResource(item.getBg());
		imageLoader.DisplayImage(item.getBg(), holder.bg);

		return row;
	}

	static class RecordHolder {
		TextView txtTitle;
		TextView Komen;
		RatingBar Rating;
		RelativeLayout bg;
		ImageView iv;
	}
}
